<template>
  <div class="xtx-goods-page" v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{
          goods.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{
          goods.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <goods-image :images="goods.mainPictures" />
          <goods-sales />
        </div>
        <div class="spec">
          <!-- 名字区组件 -->
          <goods-name :goods="goods" />
          <!-- 规格组件 -->
          <goods-sku :goods="goods" @change="changeSku" />
          <!-- 数量组件 -->
          <xtx-numbox v-model="num" :label="`数量`" :max="goods.inventory" />
          <!-- 按钮组件 -->
          <xtx-button
            type="primary"
            style="margin-top: 20px"
            @click="insertCart"
            >加入购物车</xtx-button
          >
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goods.id" />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <goods-tabs :goods="goods" />
          </div>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <goods-warn />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <goods-hot v-for="i in 3" :goodsId="goods.id" :type="i" />
        </div>
      </div>
    </div>
  </div>
  <div class="container-none" v-else></div>
</template>

<script>
import { ref } from "@vue/reactivity";
import GoodsRelevant from "./components/goods-relevant";
import { useRoute } from "vue-router";
import {
  getCurrentInstance,
  nextTick,
  provide,
  watch,
} from "@vue/runtime-core";
import { findGoods } from "@/api/product";
import GoodsImage from "./components/goods-image.vue";
import GoodsName from "./components/goods-name.vue";
import GoodsSales from "./components/goods-sales.vue";
import GoodsSku from "./components/goods-sku.vue";
import GoodsTabs from "./components/goods-tabs.vue";
import GoodsHot from "./components/goods-hot.vue";
import GoodsWarn from "./components/goods-warn.vue";
import { useStore } from "vuex";
import Message from "@/components/library/Message";

export default {
  name: "XtxGoodsPage",
  components: {
    GoodsRelevant,
    GoodsImage,
    GoodsName,
    GoodsSales,
    GoodsSku,
    GoodsTabs,
    GoodsHot,
    GoodsWarn,
  },
  setup() {
    const goods = usegoods();
    // sku改变时候触发
    provide("goods", goods);
    const changeSku = (sku) => {
      if (sku.skuId) {
        goods.value.price = sku.price;
        goods.value.oldPrice = sku.oldPrice;
        goods.value.inventory = sku.inventory;
        currSku.value = sku;
      } else {
        currSku.value = null;
      }
    };
    // 选择的数量
    const num = ref(1);

    // 加入购物车逻辑
    const currSku = ref(null);
    const store = useStore();
    const instance = getCurrentInstance();
    const insertCart = () => {
      if (!currSku.value) {
        Message({ text: "请选择商品规格" });
      } else {
        store
          .dispatch("cart/insertCart", {
            id: goods.value.id,
            skuId: currSku.value.skuId,
            name: goods.value.name,
            picture: goods.value.mainPictures[0],
            price: currSku.value.price,
            nowPrice: currSku.value.price,
            count: num.value,
            attrsText: currSku.value.specsText,
            selected: true,
            isEffective: true,
            stock: currSku.value.inventory,
          })
          .then(() => {
            Message({ type: "success", text: "添加成功" });
          });
      }
    };
    return { goods, changeSku, num, insertCart };
  },
};
// 获取商品详情
const usegoods = () => {
  const goods = ref(null);
  const route = useRoute();
  watch(
    () => route.params.id,
    (newVal) => {
      if (newVal && route.path === `/product/${newVal}`) {
        findGoods(route.params.id).then(({ result }) => {
          // 让商品数据为null然后使用v-if的组件可以重新销毁和创建
          goods.value = null;
          nextTick(() => {
            goods.value = result;
          });
        });
      }
    },
    { immediate: true }
  );
  return goods;
};
</script>

<style scoped lang='less'>
.container-none {
  height: 700px;
  width: 100%;
  background-color: #ccc;
}
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
/* /deep/.goods-tabs {
  min-height: 600px;
  background: #fff;
} */
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>